package com.swotxu.rttest.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

import java.io.IOException;

/**
 * 自定义拦截器，将原始 HttpRequest 替换为自定义的 MyHttpRequest
 *
 * @Date: 2020/9/22 23:00
 * @Author: swotXu
 */
@Slf4j
public class MyInterceptor implements ClientHttpRequestInterceptor {
    @Override
    public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
        log.info("===== 进入自定义拦截器实现 =====");
        log.info("  oldUri: {}", httpRequest.getURI());
        MyHttpRequest myHttpRequest = new MyHttpRequest(httpRequest);
        log.info("  newUri: {}", myHttpRequest.getURI());
        return clientHttpRequestExecution.execute(myHttpRequest, bytes);
    }
}
